#-*- coding: UTF-8 -*-
import redis
import MySQLdb.cursors
import time


USER_ALL_FOF_INDEX = 'fof:user:fof:temp:all:%s'
USER_PUBLIC_FOF_INDEX = 'fof:user:fof:temp:public:%s'
USER_PRIVATE_FOF_INDEX = 'fof:user:fof:temp:private:%s'
USER_COMPANY_FOF_INDEX = 'fof:user:fof:temp:company:%s'
USER_SPECIFY_FOF_INDEX = 'fof:user:fof:temp:specify:%s'
USER_SHARE_FOF_INDEX = 'fof:user:fof:temp:share:%s'
USER_MY_FOF_INDEX = 'fof:user:fof:temp:my:%s'

USER_ALL_FOF_INDEX1 = 'fof:user:fof:all:%s'
USER_PUBLIC_FOF_INDEX1 = 'fof:user:fof:public:%s'
USER_PRIVATE_FOF_INDEX1 = 'fof:user:fof:private:%s'
USER_COMPANY_FOF_INDEX1 = 'fof:user:fof:company:%s'
USER_SPECIFY_FOF_INDEX1 = 'fof:user:fof:specify:%s'
USER_SHARE_FOF_INDEX1 = 'fof:user:fof:share:%s'
USER_MY_FOF_INDEX1 = 'fof:user:fof:my:%s'




conn= MySQLdb.connect(
    host='127.0.0.1',
    port = 3306,
    user='root',
    passwd='01827746521',
    db ='fof',
    cursorclass = MySQLdb.cursors.DictCursor,
    charset="utf8"
)
cur = conn.cursor()

r = redis.Redis(host='localhost',port=6679,db=0,password='01827746521')

def renameCache(uid):
    if r.exists(USER_ALL_FOF_INDEX%uid):
        r.rename(USER_ALL_FOF_INDEX%uid,USER_ALL_FOF_INDEX1%uid)
    if r.exists(USER_PUBLIC_FOF_INDEX%uid):
        r.rename(USER_PUBLIC_FOF_INDEX%uid,USER_PUBLIC_FOF_INDEX1%uid)
    if r.exists(USER_PRIVATE_FOF_INDEX%uid):
        r.rename(USER_PRIVATE_FOF_INDEX%uid,USER_PRIVATE_FOF_INDEX1%uid)
    if r.exists(USER_COMPANY_FOF_INDEX%uid):
        r.rename(USER_COMPANY_FOF_INDEX%uid,USER_COMPANY_FOF_INDEX1%uid)
    if r.exists(USER_SPECIFY_FOF_INDEX%uid):
        r.rename(USER_SPECIFY_FOF_INDEX%uid,USER_SPECIFY_FOF_INDEX1%uid)


def getAllUserId():
    getAllUserIdSql = "select id from fof_user_info"
    cur.execute(getAllUserIdSql)
    allRows = cur.fetchall()
    uidList = []
    for row in allRows:
        uidList.append(row['id'])
    return uidList
company2UsersDict = {};
user2CompanyDict = {}

def getUserCompanyDict():
    sql = "select id,companyId from fof_user_info"
    cur.execute(sql)
    allRows = cur.fetchall()
    for row in allRows:
        user2CompanyDict[row['id']] = row['companyId']
        companyUserList = company2UsersDict.get(row['companyId'])
        if not companyUserList:
            company2UsersDict[row['companyId']] = []
        company2UsersDict[row['companyId']].append(row['id'])



def getAllPublicFof():
    getAllPublicFofSql = "select id,userId,createDate from fof_info where status = 1"
    cur.execute(getAllPublicFofSql)
    allRows = cur.fetchall()
    # fofIdList = []
    # for row in allRows:
    #     fofIdList.append(row['id'])
    # return fofIdList
    return allRows

def getCompanyFof():
    getCompanyFofSql = "select id,userId,createDate from fof_info where status = 2"
    cur.execute(getCompanyFofSql)
    allRows = cur.fetchall()
    return allRows

def getSpecifyFof():
    getSpecifyFofSql = "select id,userId,userId,createDate from fof_info where status = 3"
    cur.execute(getSpecifyFofSql)
    allRows = cur.fetchall()
    return allRows

def getSpecifyFofUsers():
    getSpecifyFofUsersSql = "select userId,fofId from fof_user";
    cur.execute(getSpecifyFofUsersSql)
    allRows = cur.fetchall()
    specifyFofUsersDict = {}
    for row in allRows:
        if not specifyFofUsersDict.get(row['fofId']):
            specifyFofUsersDict[row['fofId']] = []
        specifyFofUsersDict[row['fofId']].append(row['userId'])
    return specifyFofUsersDict

def getPrivateFof(uid):
    getPrivateFofSql = 'select id,createDate from fof_info where userId = %s and status = 4' % (uid)
    cur.execute(getPrivateFofSql)
    allRows = cur.fetchall()
    return allRows



def indexPrivateFof(uid):
    privateFofList = getPrivateFof(uid)
    for privateFof in privateFofList:
        fofDate = privateFof['createDate']
        r.zadd(USER_PRIVATE_FOF_INDEX%uid,privateFof['id'], time.mktime(fofDate.timetuple()))
        r.zadd(USER_ALL_FOF_INDEX%uid,privateFof['id'], time.mktime(fofDate.timetuple()))
        r.zadd(USER_MY_FOF_INDEX%uid,privateFof['id'], time.mktime(fofDate.timetuple()))

def indexPublicFof(uid):
    allPublicFof = getAllPublicFof()
    for publicFof in allPublicFof:
        fofDate = publicFof['createDate']
        r.zadd(USER_PUBLIC_FOF_INDEX%uid,publicFof['id'], time.mktime(fofDate.timetuple()))
        r.zadd(USER_ALL_FOF_INDEX%uid,publicFof['id'], time.mktime(fofDate.timetuple()))
        if publicFof['userId'] != uid:
            r.zadd(USER_SHARE_FOF_INDEX%uid,publicFof['id'], time.mktime(fofDate.timetuple()))
        else:
            r.zadd(USER_MY_FOF_INDEX%uid,publicFof['id'], time.mktime(fofDate.timetuple()))

def indexCompanyFof(uid,userList,fofId,fofDate):
    for companyUid in userList:
        r.zadd(USER_COMPANY_FOF_INDEX%companyUid,fofId, time.mktime(fofDate.timetuple()))
        r.zadd(USER_ALL_FOF_INDEX%companyUid,fofId, time.mktime(fofDate.timetuple()))
        if uid != companyUid:
            r.zadd(USER_SHARE_FOF_INDEX%companyUid,fofId, time.mktime(fofDate.timetuple()))
        else:
            r.zadd(USER_MY_FOF_INDEX%companyUid,fofId, time.mktime(fofDate.timetuple()))

def indexSpecifyFof(uid,userList,fofId,fofDate):
    for specifyUid in userList:
        r.zadd(USER_SPECIFY_FOF_INDEX%specifyUid,fofId, time.mktime(fofDate.timetuple()))
        r.zadd(USER_ALL_FOF_INDEX%specifyUid,fofId, time.mktime(fofDate.timetuple()))
        if uid != specifyUid:
            r.zadd(USER_SHARE_FOF_INDEX%specifyUid,fofId, time.mktime(fofDate.timetuple()))
        else:
            r.zadd(USER_MY_FOF_INDEX%specifyUid,fofId, time.mktime(fofDate.timetuple()))



getUserCompanyDict()

allUserId = getAllUserId()
for uid in allUserId:
   #将公开的FOF存入用户的索引中
    indexPublicFof(uid)
    indexPrivateFof(uid)

companyFofList = getCompanyFof()
for companyFof in companyFofList:
    uid = companyFof['userId']
    fofId = companyFof['id']
    fofDate = companyFof['createDate']
    companyId = user2CompanyDict.get(uid)
    if not companyId:
        continue
    userList = company2UsersDict.get(companyId)
    if not userList:
        continue
    indexCompanyFof(uid,userList,fofId,fofDate)

specifyFofList = getSpecifyFof()
specifyFofUsersDict = getSpecifyFofUsers()
for specifyFof in specifyFofList:
    uid = specifyFof['userId']
    fofId = specifyFof['id']
    fofDate = specifyFof['createDate']
    specifyFofUsers = specifyFofUsersDict.get(fofId)
    if not specifyFofUsers:
        continue
    indexSpecifyFof(uid,specifyFofUsers,fofId,fofDate)


for uid in allUserId:
    renameCache(uid)





